﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmAllM : Form
    {
        public FrmAllM()
        {
            InitializeComponent();
            FrmClos.bridegeOfForm1 = this;
            initialData();
        }

        #region 初始化数据
        private void initialData()
        {
            //初始化影片
            MovieManage.InitialMovie();
            //MovieManage.SerializeMovie();
            //出售化角色
            RoleManage.InitialRole();
            //初始化已购买的票
            TicketManage.InitialTicket();
        }
        #endregion

        #region 加载事件
        private void FrmCopy_Load(object sender, EventArgs e)
        {
            label9.Parent = pictureBox7;
            DataGridViewButtonColumn button = new DataGridViewButtonColumn();
            button.Text = "查看详情";
            button.UseColumnTextForButtonValue = true;
            this.dataGridView1.Columns.AddRange(button);
            pictureBox2.Parent = pictureBox7;

            if (RoleManage.user == null)
            {
                button12.Text = "点击登录";
                if (RoleManage.admin != null)
                    button12.Text = "切换登录";
                button12.Visible = true;
                pictureBox2.Visible = false;
            }
            else 
            {
                pictureBox2.Visible = true;
                button12.Visible = false;
            }

            lbl = label1;
            label1.BackColor = Color.OrangeRed;
            label1.Text = "1";

            ImageList il = new ImageList();
            //设置高度
            il.ImageSize = new Size(1, 40);
            //绑定listView控件
            listView1.SmallImageList = il;
            BindData();
        }
        #endregion

        #region 翻页切换
        Label lbl = new Label();
        private void label1_Click(object sender, EventArgs e)
        {
            lbl.BackColor = Color.Transparent;
            lbl.Text = "";
            if (sender.Equals(label1))
            {
                lbl = label1;
                label1.BackColor = Color.OrangeRed;
                label1.Text = "1";
            }
            else if (sender.Equals(label3))
            {
                lbl = label3;
                label3.BackColor = Color.OrangeRed;
                label3.Text = "2";
            }
            else if (sender.Equals(label4))
            {
                lbl = label4;
                label4.BackColor = Color.OrangeRed;
                label4.Text = "3";
            }
            else if (sender.Equals(label5))
            {
                lbl = label5;
                label5.BackColor = Color.OrangeRed;
                label5.Text = "4";
            }
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = new BindingList<Movie>(MovieManage.dirMovie.Values.ToList());
            
        }
        #endregion

        #region 查找
        /// <summary>
        /// 临时集合 存储查找的电影
        /// </summary>
        Dictionary<string, Movie> dirmovie = new Dictionary<string, Movie>();
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            dirmovie.Clear();
            if (textBox1.Text.Trim().Length < 1)
            {
                BindData();
                return;
            }
            string name = textBox1.Text.Trim();
            foreach (Movie item in MovieManage.dirMovie.Values)
            {
                if (item.MovieName.IndexOf(name) != -1)
                {
                    dirmovie.Add(item.MovieId, item);
                    break;
                }
            }
            dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = new BindingList<Movie>(dirmovie.Values.ToList());
            
        }
        #endregion

        #region 单击内容时发生
        //单击内容时发生
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                //创建dgv控件列
               
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn)
                {
                    string name=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                   
                    foreach (Movie item in MovieManage.dirMovie.Values)
                    {

                        if (item.MovieName == name)
                        {
                            FrmDetails deta = new FrmDetails();
                            deta.movie = item;
                            deta.Show();
                            break;
                        }
                    }
                }
            }
        }
        #endregion

        //关闭
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //点击登录/切换登录
        private void button12_Click(object sender, EventArgs e)
        {
            FrmLogin logic = new FrmLogin();
            logic.Show();
            this.Hide();
        }
        //个人中心
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            FrmMyCenter my = new FrmMyCenter();
            my.Show();
        }

        private void FrmAllM_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

     
    }
}
